package com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.dao.*;
import com.skytech.tax.customsdatawarehouse.singlewindow.revokelist.domain.entity.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lianhong
 * @since 2021/11/9
 */
@Service
public class CusAutoRevokeListService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CusAutoRevokeListService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

//    @Resource
//    private CusAutoRevokeListDao cusAutoRevokeListDao;
//
//    @Resource
//    private CusAutoRevokeListSimpleDao cusAutoRevokeListSimpleDao;

    @Resource
    private RevokeListDao revokeListDao;
    @Resource
    private RevokeListSimpleDao revokeListSimpleDao;

    @Resource
    private CusAutoRevokeListFileDao cusAutoRevokeListFileDao;

    public String save(String revokeList) {
        if (StringUtils.isEmpty(revokeList)) return StringUtils.EMPTY;
        String revokeListText = SecurityUtil.decrypt3DES(token, revokeList);
        try {
            RevokeList list = JSON.parseObject(revokeListText, RevokeList.class);
            if (list == null) return StringUtils.EMPTY;

            list.addField("2");
            RevokeListSimple simple = list.buildRevokeListSimple();
            if (simple == null) return StringUtils.EMPTY;

            this.remove(list.getDecModSeqNo());

//            cusAutoRevokeListDao.save(list);
//            cusAutoRevokeListSimpleDao.save(simple);

            revokeListDao.save(list);
            revokeListSimpleDao.save(simple);
            return SecurityUtil.encrypt3DES(token, list.getRevokeList());
        } catch (Exception e) {
            LOGGER.error("保存修撤单错误", e);
            return StringUtils.EMPTY;
        }
    }

    public String details(String decModSeqNo) {
        RevokeList revokeList = revokeListDao.findByDecModSeqNo(decModSeqNo);
        if (revokeList == null) return StringUtils.EMPTY;
        return SecurityUtil.encrypt3DES(token, revokeList.getRevokeList());
    }

    public void remove(String decModSeqNo) {
        revokeListDao.delete(decModSeqNo);
        revokeListSimpleDao.delete(decModSeqNo);
    }

    public boolean exists(String decModSeqNo) {
        return revokeListSimpleDao.exists(decModSeqNo);
    }

    public String existsRevokeList(List<String> decModSeqNos) {
        if (CollectionUtils.isEmpty(decModSeqNos)) return StringUtils.EMPTY;
        List<List<String>> parts = Lists.partition(decModSeqNos, 500);
        List<String> result = new ArrayList<>();
        parts.forEach(item -> result.addAll(revokeListSimpleDao.exists(item)));
        return StringUtils.join(result, ",");
    }

    public String file(String decModSeqNo) {
        return cusAutoRevokeListFileDao.findByDecModSeqNo(decModSeqNo);
    }

    public void saveFile(String decModSeqNo, String storedFile) {
        CusAutoRevokeListFile file = new CusAutoRevokeListFile(decModSeqNo, storedFile);
        cusAutoRevokeListFileDao.save(file);
    }

    public boolean fileStored(String decModSeqNo) {
        return cusAutoRevokeListFileDao.exists(decModSeqNo);
    }
}
